package com.ccpdown.view;

import java.util.List;

import com.ccpdown.view.R;
import com.ccpdown.adapter.IconfiedTextListAdapter;
import com.ccpdown.adapter.SoftListAdapter;
import com.ccpdown.layout.SoftListLayout;
import com.ccpdown.util.StringUtil;
import com.ccpdown.util.xml.CtrXmlUtil;
import com.ccpdown.vo.ConfiedText;
import com.ccpdown.vo.Software;

import android.app.Activity;
import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

public class SoftList extends ListActivity {

	// 控件属性；
	private TextView tvTName;
	private TextView tvFrontP;
	private TextView tvNextP;
	private TextView tvTilList;
	private TextView tvPageMsg;

	private List<Software> item;
	private String tid;
	private int page;
	private int totalPage;
	private int countNum;
	private com.ccpdown.vo.SoftList softs;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.softlist);
		findview();
		doing();
	}

	private void findview() {
		tvTName = (TextView) findViewById(R.id.tvTName);
		tvFrontP = (TextView) findViewById(R.id.tvFrontP);
		tvNextP = (TextView) findViewById(R.id.tvNextP);
		tvTilList = (TextView) findViewById(R.id.tvTilList);
		tvPageMsg = (TextView) findViewById(R.id.tvPageMsg);
	}

	private void doing(){
		Bundle bundle = getIntent().getExtras();
		tid = bundle.getString("typeId");
		page = bundle.getInt("page", 1);		
		showing(StringUtil.ROOTURL+StringUtil.TYPEURL+ tid+ "&page=" + page);
	}
	private void showing(String path){
		softs=CtrXmlUtil.getSoftList(path);
		totalPage=softs.totalPage;
		countNum=softs.countNum;
		tvTilList.setText(softs.parentType);
		tvTName.setText(softs.typeName+"："+softs.countNum+"款软件");
		try {
			item=softs.sws;
			if (item != null) {
				SoftListAdapter adapter = new SoftListAdapter(this);
				adapter.setItem(item);
				setListAdapter(adapter);
			}
		} catch (NullPointerException ex) {
			ex.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}
		tvFrontP.setOnClickListener(tvFrontClick);
		tvNextP.setOnClickListener(tvNextClick);
		tvPageMsg.setText(page+"/"+totalPage);
	}

	// 监听；
	@Override
	protected void onListItemClick(ListView l, View v, int position, long id) {
		// TODO Auto-generated method stub
		super.onListItemClick(l, v, position, id);
		String softId = item.get((int) id).getSoftId();
		Intent intent = new Intent();
		Bundle bundle = new Bundle();
		bundle.putString("softId", softId);
		intent.putExtras(bundle);
		intent.setClass(SoftList.this, DownloadView.class);
		startActivity(intent);
	}
	
	private OnClickListener tvFrontClick = new TextView.OnClickListener() {
		
		@Override
		public void onClick(View v) {
			if(page>1){
				page-=1;
				showing(StringUtil.ROOTURL+StringUtil.TYPEURL+ tid+ "&page=" + page);
			}
			
		}
	};
	
	private OnClickListener tvNextClick = new TextView.OnClickListener() {
		
		@Override
		public void onClick(View v) {
			if(page>=1&&page<totalPage){
				page+=1;
				showing(StringUtil.ROOTURL+StringUtil.TYPEURL+ tid+ "&page=" + page);
			}
		}
	};
}
